package com.example.common.utils;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * @program: springsecurity-study
 * @description:
 * @author: ChenZhiXiang
 * @create: 2019-07-28 17:07
 **/
@Slf4j
public class RequestContextUtil {

    public static ServletRequestAttributes getRequestAttributes() {
        return (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    }


    /**
     * 获取Request
     * @return
     */
    public static HttpServletRequest getRequest() {
//TODO:单元测试的时候，还是会得到requestAttributes，不等于null
        ServletRequestAttributes requestAttributes = getRequestAttributes();
        if (requestAttributes != null) {
            return requestAttributes.getRequest();
        } else {
            return null;
        }
    }


    /**
     * 获取Response
     * @return
     */
    public static HttpServletResponse getResponse() {
//TODO:单元测试的时候，还是会得到requestAttributes，不等于null
        ServletRequestAttributes requestAttributes = getRequestAttributes();
        if (requestAttributes != null) {
            return requestAttributes.getResponse();
        } else {
            return null;
        }
    }


    /**
     * 获取SessionId
     * @return
     */
    public static String getSessionId() {
//TODO:单元测试的时候，还是会得到requestAttributes，不等于null
        ServletRequestAttributes requestAttributes = getRequestAttributes();
        if (requestAttributes != null) {
            return requestAttributes.getSessionId();
        } else {
            return null;
        }
    }


    /**
     * 往前端写数据
     * @param object
     */
    public static void writeToResponse(Object object) {
        PrintWriter writer = null;
        try {
            HttpServletResponse response = RequestContextUtil.getResponse();
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-type", "text/html;charset=utf-8");
            writer = response.getWriter();
            writer.write(JSON.toJSONString(object));
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            writer.close();
        }
    }

    /**
     *  使用response输出JSON
     * @param response
     * @param resultMap
     */
    public static void out(HttpServletResponse response, Map<String, Object> resultMap){

        ServletOutputStream out = null;
        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json;charset=UTF-8");
            out = response.getOutputStream();
            out.write(JSON.toJSONBytes(resultMap));
        } catch (Exception e) {
            log.error(e + "输出JSON出错");
        } finally{
            if(out!=null){
                try {
                    out.flush();
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
